<template>
  <el-dialog
    ref="selectHolidayLayer"
    title="选择节假日方案"
    width="400px"
    :visible.sync="params.visible"
    @close="params.visible = false"
  >
    <compTable
      ref="table"
      :show-search="false"
      :show-icon="true"
      :icon-prop="'title'"
      :table-header="tableHeader"
      :table-data="tableData"
      :highlight-current-row="true"
      :table-height="350"
      @click-events="clickRow"
    />
    <dialog-btn slot="footer" />
  </el-dialog>
</template>

<script>
// 接收的参数 params: { id, name, isNullSelect }
import { getHolidayList } from '@/api/scheduler'
import { SQLTable } from '../../../public/js/paramarr'
export default {
  props: {
    params: {
      type: Object,
      default() {
        return {
          visible: false,
          id: '',
          name: '',
          isNullSelect: true
        }
      }
    }
  },
  data() {
    return {
      param: {},
      tableHeader: [{ prop: 'title', label: '节假日方案', sortable: 'custom' }],
      tableData: [],
      row: {}
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.param = this.params
      if (this.param.id === undefined) this.param.id = ''
      if (this.param.name === undefined) this.param.name = ''
      if (this.param.isNullSelect === undefined) this.param.isNullSelect = ''
      getHolidayList()
        .then((vPar) => {
          const rows = []
          const table = new SQLTable(vPar.getTable('k_holiday'))
          table.first()
          while (!table.eof()) {
            const row = []
            row['id'] = table.getValueByName('ID')
            row['title'] = table.getValueByName('Name')
            row['text'] = row.title
            row['icon'] = 'rili'
            rows.push(row)
            table.next()
            if (row.id === this.param.id) {
              this.$refs.table.setCurrentRow(row)
            }
          }
          this.tableData = rows
        })
    },
    clickRow(row) {
      this.row = row
    },
    confirm() {
      if (!this.param.isNullSelect && this.row.id === undefined) {
        return this.$message.warning('请选择节假日方案。')
      }
      const params = { name: this.row.title, id: this.row.id }
      if (this.params.confirm !== undefined) {
        this.params.confirm(params)
      } else {
        this.$emit('confirm', params)
      }
    }
  }
}
</script>

<style>
</style>
